package com.rcadmin.busi.admin.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.rcadmin.busi.admin.dao.SysParamDao;
import com.rcadmin.busi.admin.ddl.SysParam;
import com.rcadmin.common.exception.BusiException;
import com.rcadmin.core.jpa.DaoException;

/**
 * 系统参数服务类
 * 
 * @author tanson lam
 * @create 2016年10月13日
 */
@Component
public class SysParamService {
	@Resource
	private SysParamDao sysParamDao;

	public List<SysParam> getSysParamByRoot(String rootCode) {
		return sysParamDao.getSysParamByRoot(rootCode);
	}

	public SysParam getSysParam(String paramCode) {
		return sysParamDao.getSysParam(paramCode);
	}

	public List<SysParam> getAllSysParam() throws DaoException {
		return sysParamDao.getAllSysParam();
	}

	/**
	 * 根据指定的值获取系统参数
	 * 
	 * @param rootCode
	 * @param value
	 * @return
	 * @throws SysParamException
	 */

	public SysParam getByValue(String rootCode, String value)
			throws BusiException {
		if (StringUtils.isEmpty(rootCode) || StringUtils.isEmpty(value))
			throw new BusiException("没有匹配的系统参数");
		List<SysParam> params = sysParamDao.getSysParamByRoot(rootCode);
		for (SysParam sysParam : params) {
			if (sysParam.getParamValue().equals(value)) {
				return sysParam;
			}
		}
		throw new BusiException("没有匹配的系统参数");
	}

	/**
	 * 查询某个参数值
	 * 
	 * @param rootCode
	 * @param paramName
	 * @return
	 */
	public SysParam findByRootCodeAndParamName(String rootCode, String paramName) {
		if (StringUtils.isEmpty(rootCode) || StringUtils.isEmpty(paramName)) {
			return null;
		}
		return sysParamDao.findByRootCodeAndParamName(rootCode, paramName);
	}
}
